#include "CNativeWidget.h"
#include "CNativeHandlerDummy.h"
#include "CNativeHandlerLinux.h"
#include "CNativeHandlerMacOS.h"
#include "CNativeHandlerQt.h"
#include "CNativeHandlerWindows.h"

#include <QLayout>

CNativeWidget::CNativeWidget(QWidget *parent) : QWidget(parent) {
    m_handlerConstruct = false;
#ifdef Q_OS_WINDOWS
    m_handler = new CNativeHandlerWindows(this);
#elif defined Q_OS_MAC
    m_handler = new CNativeHandlerMacOS(this);
#elif defined Q_OS_LINUX
    m_handler = new CNativeHandlerDummy(this);
#endif
    connect(m_handler, &CNativeHandler::testMouseOnCaption, this,
            &CNativeWidget::handleHandlerTestMouse);
    connect(m_handler, &CNativeHandler::testNative, this, &CNativeWidget::handleHandlerTestNative);
    m_handlerConstruct = true;
}

CNativeWidget::~CNativeWidget() {
}

void CNativeWidget::centralize(double r) {
    m_handler->centralize(r);
}

void CNativeWidget::maximize() {
    m_handler->maximize();
}

void CNativeWidget::minimize() {
    m_handler->minimize();
}

void CNativeWidget::normalize() {
    m_handler->minimize();
}

void CNativeWidget::fullScreen() {
    m_handler->fullScreen();
}

void CNativeWidget::showForward() {
    m_handler->showForward();
}

bool CNativeWidget::resizable() const {
    return m_handler->resizable();
}

void CNativeWidget::setResizable(bool resizable) {
    m_handler->setResizable(resizable);
}

QMargins CNativeWidget::resizeMargins() const {
    return m_handler->resizeMargins();
}

void CNativeWidget::setResizeMargins(const QMargins &margins) {
    m_handler->setResizeMargins(margins);
    emit borderChanged();
}

void CNativeWidget::setResizeMargins(int margin) {
    setResizeMargins(QMargins(margin, margin, margin, margin));
}

int CNativeWidget::borderWidth() const {
    return layout() ? layout()->margin() : 0;
}

void CNativeWidget::setBorderWidth(int width) {
    layout() ? layout()->setMargin(width) : void(0);
    emit borderChanged();
}

QColor CNativeWidget::borderColor() const {
    return m_handler->borderColor();
}

void CNativeWidget::setBorderColor(const QColor &borderColor) {
    m_handler->setBorderColor(borderColor);
    emit borderChanged();
}

void CNativeWidget::handleHandlerTestMouse(bool *result, bool set) {
    if (!set) {
        *result = isMouseOnCaption();
    }
}

void CNativeWidget::handleHandlerTestNative(bool *result, bool set) {
    if (!set) {
        *result = isNative();
    } else {
        setNative(*result);
    }
}

bool CNativeWidget::isMouseOnCaption() const {
    return false;
}

bool CNativeWidget::isNative() const {
    return true;
}

void CNativeWidget::setNative(bool native) {
    Q_UNUSED(native)
}

bool CNativeWidget::event(QEvent *event) {
    // Filter event by handler prior
    if (m_handlerConstruct && m_handler->commonEventFilter(event)) {
        return true;
    }
    return QWidget::event(event);
}

bool CNativeWidget::nativeEvent(const QByteArray &eventType, void *message, long *result) {
    // Filter native event by handler prior
    if (m_handlerConstruct && m_handler->nativeEventFilter(eventType, message, result)) {
        return true;
    }
    return QWidget::nativeEvent(eventType, message, result);
}
